package com.smallkingson.ylib.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class YPatterns {

/**
 * 找到第一个匹配的字符串
 * @param str
 * @param regex
 * @return
 */
	public static String findMatchStr(String str,String regex){
		
		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(str);
		while(m.find()){
			return m.group();
		}
		return null;
	}

	public static String findMatchStr(String str,String regex,int group){

		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(str);
		while(m.find()){
			return m.group(group);
		}
		return null;
	}
	/**
	 * 把匹配的字符串转换成字符串数组
	 * @param str
	 * @param regex
	 * @return
	 */
	public static String[] findMatchStrArr(String str,String regex) {
		
		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(str);
		int size=0;
		while(m.find()){
			size++;
		}
		String[] strArr=new String[size];
		Matcher m1=p.matcher(str);
		for (int i = 0; m1.find(); i++) {
			strArr[i]=m1.group();
		}
		return strArr;
	}
	/**
	 * 
	 * @param str [c2e][c1e][c3e]
	 * @param regex c.+e
	 * @param s1 [
	 * @param s2 ]
	 * @return c1e
	 */
	public static String findStrInScope(String str,String regex,String s1,String s2,int timeOfS1,int timeOfS2){
		String str1=str;
		int start=YStrings.indexOf(str1, s1, timeOfS1);
		int end=YStrings.indexOf(str1, s2, timeOfS2);
		str=str.substring(start,end);
		str=findMatchStr(str, regex);
		return str;
	}
}
